﻿Imports EstoqueBLL
Imports VendasBLL
Public Class PermissaoCAD
    'carregar a lista dinamicamente
    'Checar oque é e o que não é
    Public usuario As Integer
    Dim op As Integer
#Region "Métodos"
#Region "Carregar Dinamicamente"
    Public Sub carregadinamico()
        Dim dt As New DataTable
        Dim menu As New MenuUsuarioBLL
        dt = menu.localizar(0, 3)
        For Each item As DataRow In dt.Rows
            Dim aux As Integer = Val(item("men_nivel").ToString)
            tvmenu.Nodes.Add(item("men_codigo").ToString, item("men_descricao"))
            tvmenu.Nodes(item("men_codigo").ToString).Tag = item("men_codigo").ToString
            Dim dt2 As New DataTable
            dt2 = menu.localizar(item("men_codigo").ToString, 4)
            For Each item2 As DataRow In dt2.Rows
                tvmenu.Nodes(item("men_codigo").ToString).Nodes.Add(item2("men_codigo").ToString, item2("men_descricao"))
                tvmenu.Nodes(item("men_codigo").ToString).Nodes(item2("men_codigo").ToString).Tag = item2("men_codigo").ToString
            Next
        Next
        tvmenu.ExpandAll()
        tvmenu.TopNode = tvmenu.Nodes(0)
    End Sub
#End Region

#Region "Carrega Permissões"
    Public Sub carregapermissao()
        Dim objusuario As New UsuarioBLL(usuario)
        TDescricao.Text = objusuario.Descricao
        Dim dt As New DataTable
        Dim menu As New MenuUsuarioBLL
        op = 100
        dt = menu.localizar(usuario, 1)
        For Each item As DataRow In dt.Rows
            For Each no1 As TreeNode In tvmenu.Nodes
                If (no1.Tag = item("men_codigo")) Then
                    no1.Checked = True
                End If
                For Each no2 As TreeNode In no1.Nodes
                    If (no2.Tag = item("men_codigo")) Then
                        no2.Checked = True
                    End If
                Next
            Next
        Next
        op = 0
    End Sub
#End Region
#End Region

#Region "Eventos"
#Region "Gravar"
    Private Sub TSBGravar_Click(sender As System.Object, e As System.EventArgs) Handles TSBGravar.Click
        Try
            ProgressBar1.Visible = True
            Dim objpermissao = New PermissaoBLL
            objpermissao.ususario = usuario
            objpermissao.excluiritens()
            For Each item As TreeNode In tvmenu.Nodes
                ProgressBar1.Value += 10
                If (item.Checked) Then
                    Dim obj As New MenuUsuarioBLL
                    obj.men_codigo = item.Tag
                    obj.usu_codigo = usuario
                    obj.inserir()
                End If
                For Each item2 As TreeNode In item.Nodes
                    If (item2.Checked) Then
                        Dim obj As New MenuUsuarioBLL
                        obj.men_codigo = item2.Tag
                        obj.usu_codigo = usuario
                        obj.inserir()
                    End If
                Next
            Next
            ProgressBar1.Visible = False
            ProgressBar1.Value = 0
            'objpermissao.InserirItens()
            MessageBox.Show("Permissão Gravada", "Finalização", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.Close()
        Catch ex As Exception
            ProgressBar1.Visible = False
            ProgressBar1.Value = 0
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
       
    End Sub
#End Region

#Region "Load"
    Private Sub PermissaoCAD_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'carregar lista dinamicamente
        'carregar permissoes apartir do usuario
        carregadinamico()
        If (usuario > 0) Then
            carregapermissao()
        End If
    End Sub
#End Region

#Region "TVMenu_AfterCheck"
    Private Sub tvmenu_AfterCheck(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles tvmenu.AfterCheck
        Dim aux As Integer = 0
        If (op <> 100) Then
            If e.Node.Checked Then
                For Each item As TreeNode In e.Node.Nodes
                    item.Checked = True
                Next
            Else
                For Each item As TreeNode In e.Node.Nodes
                    item.Checked = False
                Next
            End If
        End If
    End Sub
#End Region

#Region "TSBVoltar_Click"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#End Region
End Class

'aux = 0
'        For Each item As TreeNode In e.Node.Nodes
'            If item.Checked Then aux += 1
'        Next
'        If (aux >= 9) Then
'            e.Node.Checked = True
'        End If
